package nuc.soft.A01.jzs.controller.problem;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import nuc.soft.A01.jzs.model.general.GeneralUser;
import nuc.soft.A01.jzs.model.problem.ManyChooseProblem;
import nuc.soft.A01.jzs.model.problem.SimpleCommit;
import nuc.soft.A01.jzs.model.user.Admin;
import nuc.soft.A01.jzs.model.user.Teacher;
import nuc.soft.A01.jzs.services.AdminService;
import nuc.soft.A01.jzs.services.TeacherService;
import nuc.soft.A01.jzs.services.problem.SimpleService;
import nuc.soft.A01.jzs.services.problem.SimpleService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/teacher/simple")
public class SimpleCommitController {

	@Autowired
	private SimpleService scs;

	@RequestMapping("/showsearch")
	public String getSearchProblem(HttpServletRequest request,
			HttpServletResponse response, String searchname) {
		ArrayList<SimpleCommit> simples = scs.getSearchManyChoose(searchname);
		request.setAttribute("simples", simples);
		return "/problem/simple/index";
	}

	@RequestMapping("/showall")
	public String getAllproblem(HttpServletRequest request,
			HttpServletResponse response) {
		ArrayList<SimpleCommit> simples = scs.getAllSimple();
		request.setAttribute("simples", simples);
		return "/problem/simple/index";
	}

	@RequestMapping("/showadd")
	public String showadd(HttpServletRequest request,
			HttpServletResponse response) {
		return "/problem/simple/add";
	}

	@RequestMapping("/add")
	public String add(HttpServletRequest request, HttpServletResponse response,
			SimpleCommit problem) {
		HttpSession session = request.getSession();
		GeneralUser gu = (GeneralUser) session.getAttribute("user");
		try {
			Teacher teacher = (Teacher) gu;
			problem.setTeacherid(teacher.getTeacherid());
		} catch (Exception ex) {
			Admin admin = (Admin) gu;
			problem.setTeacherid("admin_" + admin.getAdminid());
		}
		scs.insert(problem);
		return "forward:/teacher/simple/showall";
	}

	@RequestMapping("/showedit")
	public String showedit(HttpServletRequest request,
			HttpServletResponse response, String theid) {

		SimpleCommit problem = scs.getASimpleById(theid);
		request.setAttribute("editproblem", problem);
		return "/problem/simple/edit";
	}

	@RequestMapping("/edit")
	public String edit(HttpServletRequest request,
			HttpServletResponse response, SimpleCommit problem) {
		HttpSession session = request.getSession();

		GeneralUser gu = (GeneralUser) session.getAttribute("user");
		try {
			Teacher teacher = (Teacher) gu;
			problem.setTeacherid(teacher.getTeacherid());
		} catch (Exception ex) {
			Admin admin = (Admin) gu;
			problem.setTeacherid("admin_" + admin.getAdminid());
		}

		scs.update(problem);
		return "forward:/teacher/simple/showall";
	}
}
